﻿using Domain.Entitys;
using Domain.Repositorys;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
	public class ERRepository : IERRepositoy
	{
		private readonly MyDbContext myDbContext;

		public ERRepository(MyDbContext myDbContext)
		{
			this.myDbContext = myDbContext;
		}

		public async Task<int> EMRUpdateAsync(ER eR)
		{
			ER err = await myDbContext.ER.FirstOrDefaultAsync(e => e.ResId == eR.ResId);
			err.Checker = eR.Checker;
			err.CheckTime = eR.CheckTime;
			err.Recomandation = eR.Recomandation;
			err.CheckStatus = eR.CheckStatus;
			myDbContext.ER.Update(err);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> ERAddAsync(ER er)
		{
			await myDbContext.ER.AddAsync(er);
			return await myDbContext.SaveChangesAsync();
		}

		public int ERSelectCount()
		{
			return myDbContext.ER.Count();
		}

		public async Task<List<ER>> ERSelectSie(int flid, int ncid, string name, DateTime time, DateTime timimo, string CheckStatus, int pageIndex, int pageSize)
		{
			DateTime t1 = new DateTime();
			Console.WriteLine(t1);

			//return myDbContext.ERs.FirstOrDefaultAsync(c => c.RegistTime >= time && c.RegistTime <= timimo);
			var query = myDbContext.ER.Where(c => c.CheckStatus == CheckStatus);

			if (time != t1 && timimo != t1)

				query = query.Where(c => c.HumanBirthday >= time && c.HumanBirthday <= timimo);

			// 添加其他过滤条件（根据实际字段名调整）
			if (flid != 0)
				query = query.Where(c => c.HumanMajorKindId == flid);

			if (ncid != 0)
				query = query.Where(c => c.HumanMajorId == ncid);

			if (!string.IsNullOrEmpty(name))
				query = query.Where(c => c.HumanName.Contains(name));


			// 分页逻辑
			return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
		}

		public async Task<int> ERUpdateSX2ID(ER er)
		{
			ER err = await myDbContext.ER.FirstOrDefaultAsync(e => e.ResId == er.ResId);
			err.PassPassComment = er.PassPassComment;
			myDbContext.ER.Update(err);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> ERUpdateSXID(ER er)
		{
			ER err = await myDbContext.ER.FirstOrDefaultAsync(e => e.ResId == er.ResId);
			err.PassCheckComment = er.PassCheckComment;
			err.Status = er.Status;
			myDbContext.ER.Update(err);
			return await myDbContext.SaveChangesAsync();
		}

		public async Task<int> ERUpdateZTid(ER er)
		{
			ER err = await myDbContext.ER.FirstOrDefaultAsync(e => e.ResId == er.ResId);
			err.InterviewStatus = er.InterviewStatus;
			myDbContext.ER.Update(err);
			return await myDbContext.SaveChangesAsync();
		}

		public Task<ER> SelectId(int id)
		{
			return myDbContext.ER.FirstOrDefaultAsync(c => c.ResId == id);

		}

		public async Task<List<ER>> SelectLYCX(string PassCheckComment, string PassPassComment, int pageIndex, int pageSize)
		{
			var query = myDbContext.ER.Where(e => e.PassCheckComment == PassCheckComment || e.PassCheckComment == PassPassComment);
			return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
		}

		public async Task<List<ER>> SelectLYSQ(string PassCheckComment, int pageIndex, int pageSize)
		{
			var query = myDbContext.ER.Where(e => e.PassCheckComment == PassCheckComment && e.Status == false);
			return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
		}

		public async Task<List<ER>> SelectMSZTId(int id, int pageIndex, int pageSize)
		{
			string ZTid = id.ToString();
			var query = myDbContext.ER.Where(c => c.InterviewStatus == ZTid);
			return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
		}

		public async Task<List<ER>> SelectZTId(int id, int pageIndex, int pageSize)
		{
			string ZTid = id.ToString();
			var query = myDbContext.ER.Where(c => c.CheckStatus == ZTid);
			return await query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
		}

        public int ERSelectString2Count(string PassCheckComment, string PassPassComment)
        {
            var query = myDbContext.ER.Where(c => c.PassCheckComment == PassCheckComment || c.PassPassComment == PassPassComment);
            return query.Count();
        }

        public int ERSelectStringCount(string PassCheckComment)
        {
            var query = myDbContext.ER.Where(c => c.PassCheckComment == PassCheckComment);
            return query.Count();
        }
        public int ERSelectIDCount(int id)
        {
            string ZTid = id.ToString();
            var query = myDbContext.ER.Where(c => c.InterviewStatus == ZTid);
            return query.Count();
        }

        public int ERSelectCount(int flid, int ncid, string name, DateTime time, DateTime timimo, string CheckStatus)
        {
            DateTime t1 = new DateTime();
            var query = myDbContext.ER.Where(c => c.CheckStatus == CheckStatus);
            if (time != t1 && timimo != t1)
            {
                query = query.Where(c => c.HumanBirthday >= time && c.HumanBirthday <= timimo);
            }
            if (flid != 0)
            {
                query = query.Where(c => c.HumanMajorKindId == flid);
            }
            if (ncid != 0)
            {
                query = query.Where(c => c.HumanMajorId == ncid);
            }
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(c => c.HumanName.Contains(name));
            }
            return query.Count();
        }

        public async Task<int> SelectMSZTIdCount(int id)
        {
            string ZTid = id.ToString();
            return await myDbContext.ER.Where(c => c.CheckStatus == ZTid).CountAsync();
        }
    }
}
